<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/10/27
 * Time: 00:38
 */

namespace app\common\model;

use app\common\lib\exception\ApiException;
use think\Cache;

class PushServer extends Base
{
    protected $table = 'wechatadmin.system_push_server';

    /**
     *
     * @param array $map
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws ApiException
     */
    public function getPushServer($map = [])
    {
        $id = $map['id'];

        $data = false;

        if ($this->cache_on >= 1) {
            $data = Cache::get('push_server_'.$id);
        }
        if ($data === false) {
            try {
                $field = 'title,content ,alert';

                $data = $this->field($field)->findOrFail($id);

                $data = json_decode(json_encode($data, true), true);

                Cache::set('push_server_'.$id, $data, 86400);

            } catch (\Exception $e) {
                \think\Log::write('推送获取失败：' . $e->getMessage(), 'slide_error', true);

                throw new ApiException('推送获取失败');
            }
        }
        return $data;
    }
}
